<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class StdToolRelController extends Controller
{

    //标准--工器具关系 修改
    public function edit(Request $request)
    {
        $id = $request->input('id');

        if ($request->isMethod('POST')) {
            $data = $request->only(['cycle','remark']);

            try {
                //update 返回受影响的行数
                $result = DB::table('a_std_tool_rel')->where('id', $id)->update($data);
            } catch (\Throwable $e) {
                return '2';
            }

            //由于框架自身不支持响应bool值，所以转化一种形式
            return $result >= 0 ? '1' : '0';
        } else {
            //查询信息
            $sql = "SELECT
                        A.id,
                        A.std_id,
                        A.tool_id,
                        A.cycle,
                        A.ctime,
                        A.remark,
                        C.`name`,
                        B.num 
                    FROM
                        a_std_tool_rel AS A
                        LEFT JOIN a_standard AS B ON A.std_id = B.id
                        LEFT JOIN a_tool AS C ON A.tool_id = C.id 
                    WHERE
                        A.id = :id";

            $info_std_tool_rel = DB::select($sql, ['id' => $id]);
            $info_std_tool_rel = $info_std_tool_rel[0];
            return view('admin.stdToolRel.edit', compact('info_std_tool_rel'));
        }
    }
}
